<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');

class Category extends AppModel {
	public $primaryKey = 'category_id';
	public function findAllData(){
		return $this->find("all",array(
			'conditions' => array('deleted' => 0)
			));
	}

	/**
	* Get category name by id
	* @author ThanhTran
	* @since 2014-11-10
	* @return category_name
	*/
	public function findById($id){
		return $this->find('first',array(
			'conditions' => array('category_id' => $id)
			));
	}

	/**
	* Get category name short_url
	* @author ThanhTran
	* @since 2014-11-10
	* @return category_name
	*/
	public function findByShortUrl($short_url){
		return $this->find('first',array(
			'conditions' => array('short_link' => $short_url)
			));
	}

	/**
	* Get category name by category_id and root_category_id
	* @author ThanhTran
	* @since 2014-11-10
	* @return region_name
	*/
	public function findByCategoryIdAndRootCategoryId($category_id, $root_category_id){
		return $this->find('first',array(
			'conditions' => array(
									'category_id' => $category_id,
									'root_category_id' => $root_category_id
									),
			'fields' => array('category_id','category_name')
			));
	}
	
	/**
	* Get category name by category_id and root_category_id
	* @author ThanhTran
	* @since 2014-11-10
	* @return region_name
	*/
	public function findByRootCategoryId($root_category_id){
		return $this->find('all',array(
			'conditions' => array('Category.root_category_id' => $root_category_id, "Category.deleted"=>0),
			'fields' => array('category_id','category_name')
			));
	}
	
	/**
	* Get short_link , id for routes
	* @author ThanhTran
	* @since 2014-11-10
	* @return region_name
	*/
	public function findAllForRoutes(){
		$sql = "SELECT root_categories.id, categories.category_id, root_categories.short_link, categories.short_link ".
				"FROM categories, root_categories ".
				"WHERE	categories.root_category_id = root_categories.id and ".
				"root_categories.deleted = 0 AND categories.deleted = 0";
		return $this->query($sql);
	}
}
